package com.xuxin.pda.model;

import com.xuxin.pda.bean.CommitBean;
import com.xuxin.pda.bean.DepartmentBean;
import com.xuxin.pda.bean.TrashTypeBean;
import com.xuxin.pda.contract.TrashCollectContract;
import com.xuxin.pda.network.Retrofit;
import com.xuxin.pda.network.RetrofitApi;
import com.xuxin.pda.presenter.TrashCollectPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/12/24 0024
 */
public class TrashCollectModel implements TrashCollectContract.Model {
    @Override
    public void getDepartment(TrashCollectPresenter presenter, String departmentId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getDepartment(departmentId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<DepartmentBean>() {
                    @Override
                    public void onNext(@NonNull DepartmentBean departmentBean) {
                        if (departmentBean.isSuccess()) {
                            presenter.onSuccess(departmentBean);
                        } else {
                            presenter.onError(departmentBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void getTrashType(TrashCollectPresenter presenter) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getTrashType()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<TrashTypeBean>() {
                    @Override
                    public void onNext(@NonNull TrashTypeBean trashTypeBean) {
                        if (trashTypeBean.isSuccess()) {
                            presenter.onSuccess(trashTypeBean);
                        } else {
                            presenter.onError(trashTypeBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void commit(TrashCollectPresenter presenter, String userId, String weight, String departmentId, String categoryName,String ENum) {
        try {
            JSONObject object = new JSONObject();
            object.put("userId", userId);
            object.put("weight_Kg", weight);
            object.put("departmentId", departmentId);
            object.put("categoryName", categoryName);
            object.put("ENum", ENum);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));

            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .commit(requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DisposableObserver<CommitBean>() {
                        @Override
                        public void onNext(@NonNull CommitBean commitBean) {
                            if (commitBean.isSuccess()) {
                                presenter.onSuccess(commitBean);
                            } else {
                                presenter.onError(commitBean);
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}